import Sequelize from 'sequelize'
import db from '../database'

const StudentGrade = db.define('student_grade',{
	id:{
		type: Sequelize.INTEGER,
        autoIncrement:true,
        allowNull: false,
        primaryKey:true
	},
    sno:{
    	type:Sequelize.STRING,
    	foreignKey:true
    },
    sname:{
        type:Sequelize.STRING,
        foreignKey:true
    },
    classno:{
        type:Sequelize.STRING,
        foreignKey:true
    },
    classname:{
    	type:Sequelize.STRING,
        foreignKey:true
    },
    cno:{
    	type:Sequelize.STRING,
        foreignKey:true
    },
    cname:{
    	type:Sequelize.STRING,
        foreignKey:true
    },
    grade:{
        type:Sequelize.FLOAT,
    }
},{
	freezeTableName:false,
	tableName:'student_grade',
    updatedAt:'update_time',
    createdAt:'create_time'
});

class GradeModel {
	add(record){
		return StudentGrade.create(record);
	}

	update(record){
		return StudentGrade.update(record,{
			where:{
				id:record.id
			}
		})
	}

	delete(id){
		return StudentGrade.destroy({
			where:{
				id:id
			}
		})
	}

	findById(id){
		return StudentGrade.findById(id);
	}

    findAllData(param){
        if(param.classList){
            return StudentGrade.findAll({
                where: {
                    cno: param.cno,
                    classno: {
                        $in: param.classList
                    } 
                }
            });
        } else {
            return StudentGrade.findAll({
                where: param
            })
        }
    }

	findByPage(record){
		return StudentGrade.findAndCount({
			limit: record.page_size,
			offset: record.page_size * (record.page_no - 1)
		})
	}
}

module.exports = new GradeModel();